import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

class AtiDropdownOverlay {
  /// 显示下拉菜单并返回 OverlayEntry
  static OverlayEntry show<T>({
    required BuildContext fieldContext,
    required LayerLink layerLink,
    required List<T> options,
    required ValueChanged<T> onSelected,
    T? selectedValue,
    double offsetY = 4.0,
    double maxHeight = 200,
  }) {
    late OverlayEntry overlay;

    // 获取输入框的位置和大小
    final renderBox = fieldContext.findRenderObject() as RenderBox?;
    final width = renderBox?.size.width ?? 200;
    final height = renderBox?.size.height ?? 0;

    overlay = OverlayEntry(
      builder: (_) {
        return Positioned.fill(
          child: GestureDetector(
            behavior: HitTestBehavior.translucent,
            onTap: () => overlay.remove(),
            child: Stack(
              children: [
                CompositedTransformFollower(
                  link: layerLink,
                  showWhenUnlinked: false,
                  offset: Offset(0, height + offsetY),
                  child: Material(
                    // color: Colors.yellow,
                    borderRadius: BorderRadius.circular(10.r),
                    elevation: 8,
                    child: Container(
                      width: width,
                      padding: EdgeInsets.symmetric(vertical: 5.r),
                      constraints: BoxConstraints(maxHeight: maxHeight),
                      decoration: BoxDecoration(),
                      child: ListView.builder(
                        padding: EdgeInsets.zero,
                        shrinkWrap: true,
                        itemCount: options.length,
                        itemBuilder: (context, index) {
                          final option = options[index];
                          final isSelected = option == selectedValue;
                          return InkWell(
                            onTap: () {
                              // onSelected(option);
                              overlay.remove();
                            },
                            child: Container(
                              margin: EdgeInsets.all(0),
                              padding: EdgeInsets.symmetric(
                                vertical: 10.r,
                                horizontal: 16,
                              ),
                              // color: isSelected
                              //     ? const Color(0xFFEFF8F1)
                              //     : Colors.white,
                              // color: Colors.red,
                              child: Row(
                                mainAxisAlignment:
                                    MainAxisAlignment.spaceBetween,
                                children: [
                                  Text(
                                    option.toString(),
                                    style: TextStyle(
                                      fontSize: 14,
                                      color: isSelected
                                          ? const Color(0xFF059B4F)
                                          : Colors.black87,
                                      fontWeight: isSelected
                                          ? FontWeight.w600
                                          : FontWeight.normal,
                                    ),
                                  ),
                                  if (isSelected)
                                    const Icon(
                                      Icons.check_circle,
                                      color: Color(0xFF059B4F),
                                      size: 18,
                                    ),
                                ],
                              ),
                            ),
                          );
                        },
                      ),
                    ),
                  ),
                ),
              ],
            ),
          ),
        );
      },
    );

    Overlay.of(fieldContext).insert(overlay);
    return overlay;
  }
}
